package com.ewhale.feitengguest.widget

import android.app.Dialog
import android.content.Context

import com.ewhale.feitengguest.R
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.LogUtil
import com.simga.library.utils.glide.GlideUtil
import kotlinx.android.synthetic.main.dialog_increase_prcie.*

import java.math.BigDecimal

/**
 * 作者：SimGa
 * 日期：2020/2/26
 * 时间：11:04
 * 包名：com.ewhale.feitengguest.widget
 */
class IncreasePriceDialog(context: Context) : Dialog(context, com.simga.library.R.style.Dialog) {
    private val activity: MBaseActivity<*>
    private lateinit var  unitPrice : BigDecimal
    init {
        this.setCanceledOnTouchOutside(false)
        this.setCancelable(false)
        this.activity = context as MBaseActivity<*>
        setContentView(R.layout.dialog_increase_prcie)

        btn_commit.setOnClickListener {
            var price = et_name.text.toString()
            if (et_name.text.isEmpty()) {
                activity.showToast("请输入涨价金额")
                return@setOnClickListener
            }
            if (price.substring(0, 1) == ".") {
                price = "0$price"
            }
            if (unitPrice >= BigDecimal(price)) {
                activity.showToast("涨价金额需要超过现价")
                return@setOnClickListener
            }
            if (listenter != null) {
                listenter!!.onClickCommit(price)
            }
            dismiss()
//            LogUtil.e("price---- : " + BigDecimal(price).stripTrailingZeros().toPlainString())
        }
        iv_close.setOnClickListener {
            dismiss()
        }
    }

    fun setData( img: String, title: String, unitPrice: BigDecimal){
        this.unitPrice = unitPrice
        GlideUtil.loadPicture(img, iv_cover, R.drawable.default_image)
        tv_title.text = title
        tv_nowPrice.text = "￥" + unitPrice.stripTrailingZeros().toPlainString()
    }

    private var listenter: onClickListenter? = null

    fun setListenter(listenter: onClickListenter) {
        this.listenter = listenter
    }

    interface onClickListenter {

        fun onClickCommit(price: String)
    }
}
